<?php

require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
require_once("Event.php");

class CreateEvent extends Event
{
	//$eventName,$description,$startTime,$endTime,$startDate,$endDate,$meetingLocation,$meetingTime,$locationDetails,$carpoolTime,$carpoolLocation
	public function __construct($eventName,$eventDescription,$startTime,$endTime,$startDate,$endDate,$meetingLocation,$meetingTime,$locationDetails=null,$carpoolTime=null,$carpoolLocation=null,$m1=null,$m2=null,$m3=null,$m4=null,$m5=null)
	{
		//Assumes all the variables are sanitized
		$this->eventName = $eventName;
		$this->eventDescription = $eventDescription;
		$this->startTime = $startTime;
		$this->endTime = $endTime;
		$this->startDate = $startDate;
		$this->endDate = $endDate;
		$this->meetingLocation = $meetingLocation;
		$this->locationDetails = $locationDetails;
		$this->carpoolTime = $carpoolTime;
		$this->carpoolLocation = $carpoolLocation;
		$this->meetingTime = $meetingTime;
		$this->m1 = ($m1=="null" ? null : $m1);
		$this->m2 = ($m2=="null" ? null : $m2);
		$this->m3 = ($m3=="null" ? null : $m3);
		$this->m4 = ($m4=="null" ? null : $m4);
		$this->m5 = ($m5=="null" ? null : $m5);
		
	}

	public function register()
	{
			$res = $this->insert("INSERT INTO event 
		 	(startTime,endTime,startDate,endDate,carpoolTime,carpoolLocation,locationDetails,meetingLocation,meetingTime,name,description) VALUES 
		 	('$this->startTime','$this->endTime','$this->startDate','$this->endDate','$this->carpoolTime','$this->carpoolLocation',
		 		'$this->locationDetails','$this->meetingLocation','$this->meetingTime','$this->eventName','$this->eventDescription')");	

			$data = $this->fetch_array($this->query("SELECT eventId FROM event WHERE name='$this->eventName' AND startTime='$this->startTime' AND startDate='$this->startDate' LIMIT 1"));
			$this->eventId = $data["eventId"];



			if(!is_null($this->m1))
			{
				$this->insert("INSERT INTO manages (person,event) VALUES ($this->m1,$this->eventId)");
			}
			if(!is_null($this->m2))
			{
				$this->insert("INSERT INTO manages (person,event) VALUES ($this->m2,$this->eventId)");
			}
			if(!is_null($this->m3))
			{
				$this->insert("INSERT INTO manages (person,event) VALUES ($this->m3,$this->eventId)");
			}
			if(!is_null($this->m4))
			{
				$this->insert("INSERT INTO manages (person,event) VALUES ($this->m4,$this->eventId)");
			}
			if(!is_null($this->m5))
			{
				$this->insert("INSERT INTO manages (person,event) VALUES ($this->m5,$this->eventId)");
			}

		 if($res)
		 {
		 	return true;
		 }
		 else
		 {
		 	return false;
		 }
	}
}